// thats/form/form.js
const app = getApp(); //全局app
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsInfo: [], //商品信息
        goodsId: 0,
        hiddenForm: true,
        indicatorDots: true, //商品轮播图底部圆点
        autoplay: true, //商品轮播图自动播放
        interval: 3000, //商品轮播图切换间隔
        duration: 500, //商品轮播图切换动画时间
        slideImg: [], //幻灯片广告数据
        minusStatus: 'disabled', // 使用data数据对象设置样式名
        gotoType: 1,
        animationData: {},
        opacityData: {},
        hide: 'animathide',
       
        submitId: 0, //提交表单id
        formMoney: 0.00, //表单金额
        showcoupon: false,//是否显示付款方式
        formType: 'nopay', //是否需要支付
        region: ['河南省', '郑州市', '中原区'], //开户行地区
        areaId: 410102, //地区id
        pics: [], //图片
        goodsNums: 0,
        cart: [],
        currentKey: 0, //当前下单的商品的Key
        currentGoodsId: 0, //当前选中的商品ID
        goodsTotalMoney: '0.00', //商品总额
        originForm: [], //原始表单
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //todo 邀请人ID
        var id = options.id;
        app.db.set("formId", id);
        if (!id) {
            app.common.errorToShow("路径错误");
            return false;
        }
        var that = this;
        that.checkLogin();
        //设置title名称
        wx.setNavigationBarTitle({
            title: '套餐预约'
        });
        that.setData({
            goodsId: id
        });
        this.getGoodsInfo();

    },
    onShow() {
        if (this.data.hiddenForm && app.db.get("formId")) {
            var options = { id: app.db.get("formId") }
            //console.log("调试开始");
            //console.log(this.data.hiddenForm);
            //console.log(app.db.get("formId"));
            //console.log("调试结束");
            this.onLoad(options);
        }
    },

    //地区选择
    regionChange: function (e) {
        let province_name = e.detail.value[0];
        let city_name = e.detail.value[1];
        let county_name = e.detail.value[2];
        let postal_code = 0;
        let that = this;
        let data = {
            province_name: province_name,
            city_name: city_name,
            county_name: county_name,
            postal_code: postal_code
        }

        let regionName = [province_name, city_name, county_name];
        that.setData({
            region: regionName
        });

        var id = e.currentTarget.dataset.id;
        var items = that.data.form.items;
        app.api.getAreaId(data, function (res) {
            if (res.status) {
                for (var i = 0, len = items.length; i < len; ++i) {
                    if (items[i].id == id) {
                        items[i].default_value = res.data;
                    }
                }
                that.data.form.items = items;
                that.setData({
                    form: that.data.form
                });
            } else {
                wx.showModal({
                    title: '提示',
                    content: '地区选择出现问题，请重新选择地区',
                    showCancel: false
                });
            }
        });
    },

    //表单提交
    formSubmit(e) {
        console.log(e)
        var that = this;
        var data = e.detail.value;
        var userToken = app.db.get('userToken');
        app.api.addSubmitForm({
            data: data,
            'token': userToken
        }, function (res) {
            if (!res.status) {
                app.common.errorToShow(res.msg);
                if (res.data.need_login) {
                    //跳转首页
                    setTimeout(function () {
                        wx.switchTab({
                            url: '../../index/index'
                        })
                    }, 1500);
                }
                return false;
            }

            //表单类型判断是否需要支付，支付金额多少
            if (that.data.form.type == '1' || that.data.form.type == '2') {
                
            } else {
                that.formReset();
                app.common.errorToShow(res.msg);
                //跳转首页
                setTimeout(function () {
                    wx.switchTab({
                        url: '../../index/index'
                    })
                }, 1500);
            }
        });
    },


    //检查是否登录
    checkLogin() {
        var userToken = app.db.get('userToken');
        if (!userToken) {
            app.common.successToShow('请登录...', function () {
                wx.navigateTo({
                    url: '../../member/login/level1/level1'
                });
            });
        }
    },


    //表单清空
    formReset: function () {
        app.db.set("formId", '');
        this.data.cart = []; //初始化，刷新当前页面
        this.setData({
            form: this.data.originForm
        });
    },
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function () {
        app.db.set("formId", '');
    },


  
    //通过接口查询商品信息
    getGoodsInfo: function () {
        var token = app.db.get('userToken');
        var data = {
            id: this.data.goodsId,
            token: token
        }
        var page = this;
        app.api.goodsInfo(data, function (res) {
            page.setData({
                goodsInfo: res.data,
                productId: res.data.product.id
            });
        });
    },

})